#include "Flash.h"

/*全局函数*/
uint8_t Flash_Init(void)
{
    esp_err_t err = nvs_flash_init();
    
    //如果NVS存储不带空页 或 如果在分区表中找不到标签为"nvs"的分区
    if(err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NOT_FOUND)
    {
        Serial.println("NVS Init error!");
        return 0;
    }

    Serial.println("NVS Init success!");

    return 1;
}

void Set_Maunal_Open_Atomizer_Flag_To_Flash(uint8_t data)
{
    nvs_handle my_handle;

    //打开一个命名空间，该命名空间是可读可写
    if( nvs_open("test", NVS_READWRITE, &my_handle) == ESP_OK)
    {

        if(nvs_set_u8(my_handle, "test", data) == ESP_OK)
        {
            Serial.printf("data:%d\r\n",data);
        }
        else
        {
            Serial.println("Error set nvs!");
            return ;
        }

        if(nvs_commit(my_handle) == ESP_OK)
        {

        }
        else
        {
            Serial.println("nvs commit error");
            return ;
        }

        //关闭命名空间
        nvs_close(my_handle);
    }
    else
    {
        Serial.println("Error opening NVS handle!");
        //return ;
    }
}

void Set_Manual_Open_Fan_Flag_To_Flash(uint8_t data)
{
    nvs_handle my_handle;

    if(nvs_open("test", NVS_READWRITE, &my_handle) == ESP_OK )
    {
        if(nvs_set_u8(my_handle, "test1", data) == ESP_OK)
        {
            Serial.printf("data:%d\r\n", data);
        }
        else
        {
            Serial.println("Error set nvs!");
            return ;
        }

        if(nvs_commit(my_handle) == ESP_OK)
        {

        }
        else
        {
            Serial.println("nvs_commit error");
            return ;
        }

        //关闭命名空间
        nvs_close(my_handle);
    }
    else
    {
        Serial.println("Error opening NVS handle!");
    }
}

void Get_Maunal_Open_Atomizer_Flag_To_Flash(volatile uint8_t *data)
{
    nvs_handle my_handle;
    uint8_t ret;

    if(nvs_open("test", NVS_READWRITE, &my_handle) == ESP_OK)
    {
        if(nvs_get_u8(my_handle, "test", &ret) == ESP_OK)
        {
            Serial.printf("ret:%d\r\n",ret);
            *data = ret;
        }
        else
        {
            Serial.println("nvs get error");
            return ;
        }

        if(nvs_commit(my_handle) == ESP_OK)
        {

        }
        else
        {
            Serial.println("nvs_commit error!");
            return ;
        }

        nvs_close(my_handle);

        //return ret;
    }
    else
    {
        Serial.println("Error opening NVS handle!");
    }
}

void Get_Manual_Open_Fan_Flag_To_Flash(volatile uint8_t *data)
{
    nvs_handle my_handle;
    uint8_t ret;

    if(nvs_open("test", NVS_READWRITE, &my_handle) == ESP_OK)
    {
        if(nvs_get_u8(my_handle, "test1", &ret) == ESP_OK)
        {
            Serial.printf("ret:%d\r\n",ret);
            *data = ret;
        }
        else
        {
            Serial.println("nvs get error");
            return ;
        }

        if(nvs_commit(my_handle) == ESP_OK)
        {

        }
        else
        {
            Serial.println("nvs_commit error!");
            return ;
        }

        nvs_close(my_handle);
    } 
    else
    {
        Serial.println("Error opening NVS handle!"); 
    }
}